package com.edgardeng.education.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edgardeng.education.entity.SchoolClass;
import com.edgardeng.education.entity.JsonResponse;
import com.edgardeng.education.mapper.SchoolClassMapper;
import com.edgardeng.education.service.ISchoolClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 学校班级 前端控制器
 * </p>
 *
 * @author edgardeng
 * @since 2020-04-28
 */
@RestController
@RequestMapping("/api/school/class")
public class SchoolClassController extends BaseController {
    @Autowired
    ISchoolClassService baseService;

    @Autowired
    SchoolClassMapper schoolClassMapper;

    // http://localhost:8090/api/school/class
    @GetMapping()
    public IPage<SchoolClass> SchoolClassPage(
            @RequestParam(value = "size", required = false, defaultValue = "10") Integer size,
            @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
            SchoolClass entity) {
        Page<SchoolClass> page = new Page<>(current, size); // current page and page size
        LambdaQueryWrapper<SchoolClass> queryWrapper = new LambdaQueryWrapper<>();
        if(entity != null){
            // TODO query condition
        }
        IPage<SchoolClass> SchoolClassPage = baseService.page(page, queryWrapper);
        return SchoolClassPage;
    }

    // http://localhost:8090/api/school/class/school
    @GetMapping("school")
    public IPage<SchoolClass> classWithSchool(
            @RequestParam(value = "size", required = false, defaultValue = "10") Integer size,
            @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
            SchoolClass entity) {
        Page<SchoolClass> page = new Page<>(current, size); // current page and page size
        IPage<SchoolClass> rows = schoolClassMapper.schoolClassListWithSchool(page, entity);
        return rows;
    }
    // http://localhost:8090/api/school/class/student
    @GetMapping("student")
    public IPage<SchoolClass> classWithStudent(
            @RequestParam(value = "size", required = false, defaultValue = "10") Integer size,
            @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
            SchoolClass entity) {
        Page<SchoolClass> page = new Page<>(current, size); // current page and page size
        IPage<SchoolClass> rows = schoolClassMapper.schoolClassListWithStudent(page, entity);
        return rows;
    }

    @PostMapping
    public JsonResponse addSchoolClass(@RequestBody SchoolClass entity) {
        boolean r = baseService.save(entity);
        return JsonResponse.result(r);
    }

    @GetMapping("/{id}")
    public SchoolClass getSchoolClass(@PathVariable Long id) {
        SchoolClass r = this.baseService.getById(id);
        return r;
    }

    @DeleteMapping("/{id}")
    public JsonResponse deleteSchoolClass(@PathVariable Long id) {
        boolean r = this.baseService.removeById(id);
        return JsonResponse.result(r);
    }

    @PutMapping()
    public JsonResponse updateSchoolClass(@RequestBody SchoolClass entity) {
        boolean r = this.baseService.updateById(entity);
        return JsonResponse.result(r);
    }
}
